package com.kavin.aidl;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyService extends Service {
    public MyService() {
    }


    private final RemoteCallbackList<BookCallBack> remoteCallbackList = new RemoteCallbackList<>();
//    private final Map<IBinder, BookCallBack> callBackMap = new HashMap<>(); // 原理与 RemoteCallbackList 类似

    ServiceProxy.Stub stub = null;

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("------TAG", "onBind: " + this);
        stub = new ServiceProxy.Stub() {

            @Override
            public String setString(String str) throws RemoteException {
                return str + "  service MyService.this: " + MyService.this + "   stub: " + stub;
            }

            @Override
            public Book setBook(Book book) throws RemoteException {
                return new Book(book.getName() + 2);
            }

            @Override
            public void registerCallback(BookCallBack bc) throws RemoteException {
                IBinder binder = bc.asBinder();
                Log.d("------TAG", "registerCallback: BookCallBack: " + bc); //BookCallBack$Stub$Proxy@c687c2d  ,注册与注销的 BookCallBack 不是同一对象
                Log.d("------TAG", "unregisterCallback: binder: " + binder); //BinderProxy@620c562  注册与注销的 IBinder 是同一个
                remoteCallbackList.register(bc);
//                callBackMap.put(binder, bc);
                callback();
            }

            @Override
            public void unregisterCallback(BookCallBack bc) throws RemoteException {
                IBinder binder = bc.asBinder();
                Log.d("------TAG", "unregisterCallback: BookCallBack: " + bc);//BookCallBack$Stub$Proxy@3e416f3  ,注册与注销的 BookCallBack 不是同一对象, 所以不能直接用于 注销
                Log.d("------TAG", "unregisterCallback: binder: " + binder);  //BinderProxy@620c562  注册与注销的 IBinder 是同一个
                remoteCallbackList.unregister(bc);
//                callBackMap.remove(binder);
            }

        };
        return stub;
    }

    Handler handler = new Handler(Looper.getMainLooper());
    int count = 0;

    private void callback() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                int N = remoteCallbackList.beginBroadcast();
                try {
                    for (int i = 0; i < N; i++) {
                        remoteCallbackList.getBroadcastItem(i).getCount(count);
                    }
                    count++;
                } catch (RemoteException e) {
                    Log.e("------TAG", "setCallback: ", e);
                }
                remoteCallbackList.finishBroadcast();

                handler.postDelayed(this, 1000);
            }
        }, 1000);
    }

/*
    private void callback2() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    for (BookCallBack bookCallBack : callBackMap.values()) {
                        bookCallBack.getCount(count);
                    }
                    count++;
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                handler.postDelayed(this, 1000);
            }
        }, 1000);
    }*/


    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("------TAG", "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("------TAG", "onDestroy: ");
    }
}
